package com.wanli.storemobile.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * 类描述与备注
 *
 * @author 客尘(衡文闯)
 * @version $Id: PopWinDownUtil, v1.0.0 2018年11月13日 17:07 客尘(衡文闯) Exp $
 */
public class PopWinDownUtil {
    private Activity context;
    private View contentView;
    private View relayView;
    private PopupWindow popupWindow;

    public PopWinDownUtil(Activity context, View contentView, View relayView) {
        this.context = context;
        this.contentView = contentView;
        this.relayView = relayView;
        init();
    }

    @SuppressLint("WrongConstant")
    public void init() {
        //内容，高度，宽度
        popupWindow = new PopupWindow(contentView, ScreenUtils.getScreenWidth(context), WindowManager.LayoutParams.MATCH_PARENT, true);
        //动画效果
//        popupWindow.setAnimationStyle(R.style.AnimationTopFade);
        //菜单背景色
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        popupWindow.setClippingEnabled(false);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        //关闭事件
        popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (onDismissLisener != null) {
                    onDismissLisener.onDismiss();
                }
            }
        });
    }

    public void show() {
        //显示位置
//        popupWindow.showAsDropDown(relayView);
        showAsDropDown(popupWindow, relayView, 0, 0);
    }

    public void show(View relayView) {
        //显示位置
        showAsDropDown(popupWindow, relayView, 0, 0);
    }

    /**
     * @param pw     popupWindow
     * @param anchor v
     * @param xoff   x轴偏移
     * @param yoff   y轴偏移
     */
    public void showAsDropDown(final PopupWindow pw, final View anchor, final int xoff, final int yoff) {
//        if (Build.VERSION.SDK_INT >= 24) {
//            Rect visibleFrame = new Rect();
//            anchor.getGlobalVisibleRect(visibleFrame);
//            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
//            pw.setHeight(height);
//            pw.showAsDropDown(anchor, xoff, yoff);
//        } else {
//            pw.showAsDropDown(anchor, xoff, yoff);
//        }

        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height;
            /**
             * 判断是否有导航栏
             */
            if (ScreenUtils.isShowNavBar(pw.getContentView().getContext())) {
                height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom + Math.abs(yoff);
            } else {
                /**
                 * 获取物理真实高度 才能在android 7.0以上遮住下面的TabHost
                 */
                height = ScreenUtils.getRealHeight((Activity) pw.getContentView().getContext()) - visibleFrame.bottom + Math.abs(yoff);
            }
            pw.setWidth(ScreenUtils.getScreenWidth(context));
            pw.setHeight(height);
        }
        pw.showAsDropDown(anchor, 0, yoff);
    }

    public void hide() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    private OnDismissLisener onDismissLisener;

    public void setOnDismissListener(OnDismissLisener onDismissLisener) {
        this.onDismissLisener = onDismissLisener;
    }

    public interface OnDismissLisener {
        void onDismiss();
    }

    public boolean isShowing() {
        return popupWindow.isShowing();
    }
}
